using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using Demo.Model;
using NUnit.Framework;

using Pampa.Grid.Core;
using Pampa.Grid.Core.Filters;
using Pampa.Grid.Data;
using Pampa.Grid.UI;
using Pampa.Grid.UI.CustomControls;
using Pampa.Grid.Exceptions;


namespace Pampa.Grid.Test
{
    [TestFixture]
    public class DataTest : AbstractGridTest
    {
        private CustomGrid grid;

        [TestFixtureSetUp]
        public override void FixtureInit()
        {
            base.FixtureInit();

            // Creo y configuro la lista
            grid = new CustomGrid();
            grid.AddColumn("ID", "ID", ColumnType.String, "", "");
            grid.AddColumn("LastName", "LastName", ColumnType.String, "", "");
            grid.AddColumn("FirstName", "FirstName", ColumnType.String, "", "");
            grid.AddColumn("Limit", "Limit", ColumnType.String, "", "");
            grid.AddColumn("Nationality.Description", "Nationality", ColumnType.String, "", "");
        }

        [Test]
        public void Sql_GetData_Without_Filters()
        {
            grid.DataReader = new GridDataReaderSqlView("Pampa.Test", "Customer");

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Sql_Customers.xml");
        }


        [Test]
        public void Sql_GetData_With_Filters()
        {
            grid.DataReader = new GridDataReaderSqlView("Pampa.Test", "Customer");

            // Filtros
            Filter filterLimit1 = new SimpleFilter("Limit", OperationType.GraterEqual, 50);
            Filter filterLimit2 = new SimpleFilter("Limit", OperationType.LessEqual, 60);
            Filter filterLastName = new SimpleFilter("LastName", OperationType.StartsWith, "Nara 7");

            GroupFilter filterLimit = new AndGroupFilter(filterLimit1, filterLimit2);
            GroupFilter mainFilter = new OrGroupFilter(filterLimit, filterLastName);

            grid.Configuration.MainFilter = mainFilter;

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Sql_Customers_Filtered.xml");
        }

        [Test]
        public void Entity_GetData_Without_Filters()
        {
            grid.DataReader = new GridDataReaderEntityAR("Demo.Model.Customer, Demo.Model");

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entity_Customers.xml");
        }

        [Test]
        public void Entity_GetData_With_Filters()
        {
            grid.DataReader = new GridDataReaderEntityAR("Demo.Model.Customer, Demo.Model");

            // Filtros
            Filter filterLimit1 = new SimpleFilter("Limit", OperationType.GraterEqual, 50);
            Filter filterLimit2 = new SimpleFilter("Limit", OperationType.LessEqual, 60);
            Filter filterLastName = new SimpleFilter("LastName", OperationType.StartsWith, "Nara 7");

            GroupFilter filterLimit = new AndGroupFilter(filterLimit1, filterLimit2);
            GroupFilter mainFilter = new OrGroupFilter(filterLimit, filterLastName);

            grid.Configuration.MainFilter = mainFilter;

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entity_Customers_Filtered.xml");
        }

        [Test]
        public void Entity_Hql_GetData_With_NestedProperty_Filter()
        {
            grid.DataReader = new GridDataReaderEntityHql("Demo.Model.Customer, Demo.Model");

            // Filtros
            Filter filterNationality = new SimpleFilter("Nationality.Description", OperationType.Equal, "Argentina");

            grid.Configuration.MainFilter.AddFilter(filterNationality);

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entity_Hql_Customers_NestedProperty_Filtered.xml");
        }

        [Test]
        public void Entity_Hql_GetData_With_NestedProperty_Filter_And_NestedProperty_OrderBy()
        {
            grid.DataReader = new GridDataReaderEntityHql("Demo.Model.Customer, Demo.Model");

            // Filtros
            Filter filterNationality = new SimpleFilter("Nationality.Description", OperationType.Equal, "Argentina");

            grid.Configuration.MainFilter.AddFilter(filterNationality);
            grid.Configuration.OrderBy.Add(new KeyValuePair<string, OrderDirection>("Nationality.Description", OrderDirection.Ascending));

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entity_Hql_Customers_NestedProperty_Filtered.xml");
        }

        [Test]
        public void Entity_Hql_GetData_With_EntityProperty_Filter()
        {
            grid.DataReader = new GridDataReaderEntityHql("Demo.Model.Customer, Demo.Model");

            // Filtros
            Country argentina = Country.FindAllByProperty("Description", "Argentina")[0];
            Filter filterNationality = new SimpleFilter("Nationality", OperationType.Equal, argentina);

            grid.Configuration.MainFilter.AddFilter(filterNationality);

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entit_Hql_Customers_EntityProperty_Filtered.xml");
        }

        [Test]
        public void Entity_Hql_CountEntities_With_EntityProperty_Filter()
        {
            grid.DataReader = new GridDataReaderEntityHql("Demo.Model.Customer, Demo.Model");

            // Filtros
            Country argentina = Country.FindAllByProperty("Description", "Argentina")[0];
            Filter filterNationality = new SimpleFilter("Nationality", OperationType.Equal, argentina);

            grid.Configuration.MainFilter.AddFilter(filterNationality);

            Assert.AreEqual(1, grid.LastPage);
        }

        [Test]
        public void Entity_Hql_GetData_With_Join_Filter_Hobbies()
        {
            grid.DataReader = new GridDataReaderEntityHql("Demo.Model.Customer, Demo.Model");

            // Filtros
            SimpleFilter filterHobby1 = new SimpleFilter("Description", OperationType.Equal, "Drums");
            SimpleFilter filterHobby2 = new SimpleFilter("Description", OperationType.Equal, "Basketball");

            OrGroupFilter orGroupFilter = new OrGroupFilter();
            orGroupFilter.Join = "Hobbies";
            orGroupFilter.AddFilter(filterHobby1);
            orGroupFilter.AddFilter(filterHobby2);

            grid.Configuration.MainFilter.AddFilter(orGroupFilter);

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entity_Hql_GetData_With_Join_Filtered_Hobbies.xml");
        }

        [Test]
        public void Entity_Hql_GetData_With_Join_Filter_Phones_StartsWith()
        {
            grid.DataReader = new GridDataReaderEntityHql("Demo.Model.Customer, Demo.Model");

            // Filtros
            SimpleFilter filterPhone = new SimpleFilter("Number", OperationType.StartsWith, "1");

            AndGroupFilter andGroupFilter = new AndGroupFilter();
            andGroupFilter.Join = "Phones";
            andGroupFilter.AddFilter(filterPhone);

            grid.Configuration.MainFilter.AddFilter(andGroupFilter);

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entity_Hql_GetData_With_Join_Filtered_Phones.xml");
        }

        [Test]
        public void Entity_Hql_GetData_With_Join_Filter_Complex1()
        {
            grid.DataReader = new GridDataReaderEntityHql("Demo.Model.Customer, Demo.Model");

            // Filtros
            SimpleFilter nameFilter = new SimpleFilter("LastName", OperationType.Contains, "0");
            grid.Configuration.MainFilter = new OrGroupFilter();
            grid.Configuration.MainFilter.AddFilter(nameFilter);

            XmlDocument doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entity_Hql_GetData_With_Join_Filtered_Name_With_0.xml");

            SimpleFilter filterHobby1 = new SimpleFilter("Description", OperationType.Equal, "Drums");
            SimpleFilter filterHobby2 = new SimpleFilter("Description", OperationType.Equal, "Basketball");
            OrGroupFilter hobbiesFilter = new OrGroupFilter();
            hobbiesFilter.Join = "Hobbies";
            hobbiesFilter.AddFilter(filterHobby1);
            hobbiesFilter.AddFilter(filterHobby2);
            grid.Configuration.MainFilter.AddFilter(hobbiesFilter);

            doc = grid.TestGetXmlData();
            doc.Save(this.ResultsFolderPath + "\\Entity_Hql_GetData_With_Join_Filtered_Name_With_0_Or_Hobbies.xml");
        }

        private string ResultsFolderPath
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["ResultsFolderPath"]; }
        }
    }
}
